package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.Classes;

/**
 * @author 袁小弘
 * @describe 班级DAO层
 * @date 2019-12-31
 */
@Repository
public interface ClassDao extends JpaRepository<Classes, Integer> {

    @Transactional
    @Modifying
    int deleteByClassId(Integer classId);

    List<Classes> findByClassId(Integer classId);

    @Transactional
    @Modifying
    int deleteByClassIdIn(List<Integer> listId);

    @Query(nativeQuery = true, value = "SELECT c.*,te.termName,ct.typeName FROM (term te\r\n"
            + "		 INNER JOIN classes c  on te.term_Id = c.term_Id) INNER JOIN class_type ct \r\n"
            + "		 on c.class_type_Id = ct.class_type_Id WHERE te.termName like CONCAT('%',:termName,'%')")
    List<Map<String, Object>> findByTermNameLike(@Param("termName") String termName);

    @Query(nativeQuery = true, value = "SELECT c.*,te.termName,ct.typeName FROM (term te\r\n"
            + "		 INNER JOIN classes c  on te.term_Id = c.term_Id) INNER JOIN class_type ct \r\n"
            + "		 on c.class_type_Id = ct.class_type_Id WHERE c.className like CONCAT('%',:className,'%')")
    List<Map<String, Object>> findByClassNameLike(@Param("className") String className);

    @Query(nativeQuery = true, value = "SELECT c.*,te.termName,ct.typeName FROM (term te\r\n"
            + "		 INNER JOIN classes c  on te.term_Id = c.term_Id) INNER JOIN class_type ct \r\n"
            + "		 on c.class_type_Id = ct.class_type_Id WHERE ct.typeName like CONCAT('%',:typeName,'%')")
    List<Map<String, Object>> findByTypeNameLike(@Param("typeName") String typeName);

    @Query(nativeQuery = true, value = "SELECT c.*,te.termName,ct.typeName FROM (term te INNER JOIN classes c ON te.term_Id = c.term_Id) \r\n"
            + "INNER JOIN class_type ct ON c.class_type_Id = ct.class_type_Id ")
    List<Map<String, Object>> queryAllClassAndTerm();
}
